package org.bbelovic.devel.exercises;

/**
 * Various number methods.
 */
public abstract class NumberUtilities 
{
    /**
     * Tests primality of given number.
     * @param n number to be tested for primality
     * @return true if number is prime, false otherwise
     */
    public static boolean isPrime(final long n)
    {
        for (int i = 2; i*i <= n; i++) {
            if (n % i == 0) return false;
        }
        return true;
    }
    
    /**
     * Returns number of divisors for given number.
     * @param n number to be tested
     * @return number of divisors for given number
     */
    public static int numberOfDivisors(final long n) {
        int result = 0;
        for (int i = 1; i <= n; i++) {
            if (n % i == 0) result++;
        }
        return result;
    }
    
    /**
     * Powers given number n to exponent exp.
     * @param n number to be powered
     * @param exp exponent
     * @return result of n^exp
     */
    public static long pow(final long n, final int exp) {
        if (exp == 0) return 1L;
        else {
            long result = 1L;
            for (int i = 1; i <= exp; i++) {
                result = result * n;
            }
            return result;
        }
    }
}
